import { http } from '@/utils/http'
import { delToken, setToken } from '@/utils/token'

export const login = (loginData) => {
  return async (dispatch) => {
    const res = await http.post('/authorizations', loginData)
    const { data, message } = res.data
    if (message === 'OK') {
      dispatch({
        type: 'login/setToken',
        payload: data.token,
      })
      setToken(data.token)
    }
  }
}

export const logout = () => {
  return (dispatch) => {
    //清除token,要清除本地和redux里的
    delToken()
    dispatch({
      type: 'login/logout',
    })
    //清空用户信息
    dispatch({
      type: 'user/clearUserInfo',
    })
  }
}
